package com.devfest.wishmaster.gui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.devfest.wishmaster.R;
import com.devfest.wishmaster.model.JSONParser;
import com.devfest.wishmaster.model.WishList;

public class LoginActivity extends Activity implements OnClickListener,
		OnEditorActionListener {

	EditText passwordEditText;
	EditText userNameEditText;
	RelativeLayout progressLayout;
	private boolean backPressed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
//		moveToNext();
		
		userNameEditText = (EditText) findViewById(R.id.login_username_id);
		passwordEditText = (EditText) findViewById(R.id.login_password_id);
		progressLayout = (RelativeLayout) findViewById(R.id.login_progress_layout);
		passwordEditText.setOnEditorActionListener(this);
		findViewById(R.id.login_enter_btn_id).setOnClickListener(this);
	}

	String errorText = "";
	Thread runningthread;

	private void enter() {
		if (userNameEditText.getText().toString().trim().equals("")) {
			Toast.makeText(this, "Fill username", Toast.LENGTH_SHORT).show();
			return;
		}
		if (passwordEditText.getText().toString().trim().equals("")) {
			Toast.makeText(this, "Fill password", Toast.LENGTH_SHORT).show();
			return;
		}
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

		if (passwordEditText.hasFocus())
			imm.hideSoftInputFromWindow(passwordEditText.getWindowToken(), 0);
		if (userNameEditText.hasFocus())
			imm.hideSoftInputFromWindow(userNameEditText.getWindowToken(), 0);
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user", userNameEditText.getText()
				.toString().trim()));
		params.add(new BasicNameValuePair("pass", passwordEditText.getText()
				.toString().trim()));
		LoginActivity.this.progressLayout.setVisibility(View.VISIBLE);
		errorText = "";
		runningthread = new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					List<WishList> list = JSONParser.getInstance().getWishList(params);
					MyApplication.getInstance().wishLists = list;
				} catch (Exception e) {
					errorText = e.toString();
					Log.e("DevFest", e.toString());
				}
				if (MyApplication.getInstance().getActiveActivity() instanceof LoginActivity)
					MyApplication.getInstance().getActiveActivity()
							.runOnUiThread(new Runnable() {

								@Override
								public void run() {
									if (backPressed)
										return;
									LoginActivity.this.progressLayout
											.setVisibility(View.GONE);
									if (errorText.equals("")) {

										startActivity(new Intent(
												LoginActivity.this,
												WishListActivity.class));
										LoginActivity.this.finish();
									} else {
										Toast.makeText(
												LoginActivity.this,
												"Login Failed",
												Toast.LENGTH_SHORT).show();
										errorText = "";
									}
								}
							});
			}
		});
		backPressed = false;
		runningthread.start();
	}

	@Override
	public void onBackPressed() {
		if (runningthread != null && runningthread.isAlive()) {
			runningthread.interrupt();
			backPressed = true;
			progressLayout.setVisibility(View.GONE);
			return;
		}
		super.onBackPressed();
	}

	@Override
	protected void onResume() {
		super.onResume();
		MyApplication.getInstance().setActivity(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_enter_btn_id:
			enter();
			break;
		}
	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if (actionId == EditorInfo.IME_ACTION_GO) {
			enter();
			return true;
		}
		return false;
	}
}